package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.service.ServiceItemService;
import cn.wolfcode.car.business.service.StatementItemService;
import cn.wolfcode.car.business.service.StatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author WaveHi
 * @Date 15/11/2021 上午 8:57
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Service
public class StatementItemServiceImpl extends ServiceImpl<StatementItemMapper, StatementItem> implements StatementItemService {

//    @Autowired
//    private StatementItemMapper statementItemMapper;
//
//    @Autowired
//    private StatementMapper statementMapper;
//
//    @Autowired
//    private ServiceItemMapper serviceItemMapper;

    @Autowired
    private StatementService statementService;

    @Autowired
    private ServiceItemService serviceItemService;

    @Override
    public StatementItem getStatementItem(Long id) {
        return getById(id);
//        return statementItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public TablePageInfo<StatementItem> getAllStatementItems() {
        return new TablePageInfo<>(list());
//        return new TablePageInfo<>(statementItemMapper.selectAll());
    }

    @Override
    public TablePageInfo<StatementItem> getPartOfStatementItems(Long statementId) {
        QueryWrapper<StatementItem> statementItemQueryWrapper = new QueryWrapper<>();
        statementItemQueryWrapper
                .eq(statementId != null,
                        "statement_id",
                        statementId);
        return new TablePageInfo<>(list(statementItemQueryWrapper));
//        return new TablePageInfo<>(statementItemMapper.selectByStatementId(statementId));
    }

    @Override
    @Transactional
    public Boolean addItems(List<StatementItem> statementItems) {
        //Get statement
        BigDecimal countPrice = new BigDecimal("0");
        BigDecimal count = new BigDecimal("0");
        BigDecimal countDiscount = new BigDecimal("0");
        Boolean result = true;
//        int i = 0;
        if(statementItems != null && statementItems.size() > 0) {
            //
            StatementItem remove = statementItems.remove(statementItems.size() - 1);
            removeById(remove.getStatementId());
//            statementItemMapper.deleteByStatementId(remove.getStatementId());
            ServiceItem serviceItem = null;
            Statement statement = statementService.getStatement(remove.getStatementId());
//            Statement statement = statementMapper.selectByPrimaryKey(remove.getStatementId());
            countDiscount = countDiscount.add(remove.getItemPrice());
            for (StatementItem statementItem : statementItems) {
                //Add
                result = save(statementItem) && result;
//                i += statementItemMapper.insert(statementItem);
                serviceItem = serviceItemService.getServiceItem(statementItem.getItemId());
//                serviceItem = serviceItemMapper.selectByPrimaryKey(statementItem.getItemId());
                if(serviceItem != null) {
//                        countDiscount = countDiscount.add(serviceItem.getDiscountPrice().multiply(statementItem.getItemQuantity()));
                        count = count.add(statementItem.getItemQuantity());
                        countPrice = countPrice.add(serviceItem.getOriginalPrice().multiply(statementItem.getItemQuantity()));
                    }
            }

            statement.setTotalAmount(countPrice);
            statement.setTotalQuantity(count);
            statement.setDiscountAmount(countDiscount);
            result = statementService.update(statement);
//            i += statementMapper.updateByPrimaryKey(statement);
        }
        return  result;
//        return i > 0;
    }

    @Override
    public Boolean pay(Long statementId) {
        Statement statement = statementService.getStatement(statementId);
        User user = ShiroUtils.getUser();
        statement.setStatus(Statement.STATUS_PAID);
        statement.setPayeeId(user.getId());
        statement.setPayTime(new Date());
        return statementService.update(statement);
//        Statement statement = statementMapper.selectByPrimaryKey(statementId);
//        User user = ShiroUtils.getUser();
//        statement.setStatus(Statement.STATUS_PAID);
//        statement.setPayeeId(user.getId());
//        statement.setPayTime(new Date());
//        int i = statementMapper.updateByPrimaryKey(statement);
//        return i > 0;
    }
}
